package mobile

import (
	"fmt"
	"net/url"
	"os"
	"strconv"
	"strings"
	"tcsc/models"
	"tcsc/sword"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
	"github.com/astaxie/beego/orm"
)

// "sword"

type My struct {
	BaseController
}

func (c *My) Get() {
	var shop orm.Params
	var isshop int
	if shops := models.Query(`select id from mt_shop where style=1 and status=1 and censoring=1 and uid=` + c.Uid); shops != nil {
		t := time.Now().Local()
		ttime := t.Format("2006-01-02")
		t2 := t.AddDate(0, 0, 1)
		dtime := t2.Format("2006-01-02")
		ttime += ` 00:00:00`
		dtime += ` 00:00:00`
		for _, v := range shops {
			order := models.Query(`select count(a.id) as num,sum(a.money) as moneysum from mt_order a where a.status>1 and a.status<6 and 
					a.addtime>='` + ttime + `' and a.addtime<'` + dtime + `' and a.gid=` + v["id"].(string))
			v["num"] = order[0]["num"]
			if order[0]["moneysum"] != nil {
				v["moneysum"] = order[0]["moneysum"]
			} else {
				v["moneysum"] = "0"
			}
		}
		shop = shops[0]
		isshop = 1
	} else {
		if isapply := models.Query(`select censoring from mt_shop_add where uid=` + c.Uid + ` order by addtime desc`); isapply != nil {
			cens := fmt.Sprint(isapply[0]["censoring"])
			if cens == "0" {
				isshop = 2 //请等待
			} else if cens == "2" {
				isshop = 3 //不通过
			} else {
				isshop = 0 //未申请
			}
		} else {
			isshop = 0
		}
	}
	if fmt.Sprint(models.Systems()["uid"]) == c.Uid {
		isshop = 1
	}
	var isshop2 int
	if sh := models.Query(`select id from mt_shop where style=0 and status=1 and censoring=1 and uid=` + c.Uid); sh != nil {
		isshop2 = 1
	} else {
		isshop2 = 0
	}

	info := models.Query(`select a.id,a.monicker,a.money,a.type,a.driver,b.pathname,c.name as vname,d.pathname as vpath from mt_member a
	 left join mt_resources b on a.avatar=b.id left join mt_member_vip c on a.vip=c.id left join mt_resources d on c.img=d.id
	  where a.id=` + c.Uid)[0]
	info["typed"] = info["type"]
	//type 0普通用户 1推广员  2分销员  3待审核  4请修改
	if ispro := models.Query(`select id,censoring from mt_promote_add where uid=` + c.Uid + ` and status=1 and style=1`); ispro != nil {
		cen := fmt.Sprint(ispro[0]["censoring"])
		switch cen {
		case "0":
			info["type"] = "3"
		case "2":
			info["type"] = "4"
		}

	}
	if ispro2 := models.Query(`select id,censoring from mt_promote_add where uid=` + c.Uid + ` and status=1 and style=2`); ispro2 != nil {
		cen := fmt.Sprint(ispro2[0]["censoring"])
		switch cen {
		case "0":
			info["typed"] = "3"
		case "2":
			info["typed"] = "4"
		}
	}
	info["isshop"] = isshop
	info["isshop2"] = isshop2
	collect := models.Query(`select count(a.id) as num from mt_member_collection a where a.uid=` + c.Uid)[0]["num"]
	focus := models.Query(`select count(a.id) as num from mt_member_focus a where a.uid=` + c.Uid)[0]["num"]
	read := models.Query(`select count(a.id) as num from mt_systems_reading a where a.uid=` + c.Uid + ` and a.pid>0`)[0]["num"]
	order1 := models.Query(`select count(a.id) as num from mt_order a where a.status=1 and a.uid=` + c.Uid)[0]["num"]
	order2 := models.Query(`select count(a.id) as num from mt_order a where a.status=2 and a.uid=` + c.Uid)[0]["num"]
	order3 := models.Query(`select count(a.id) as num from mt_order a where a.status=3 and a.uid=` + c.Uid)[0]["num"]
	order4 := models.Query(`select count(a.id) as num from mt_order a where a.status=4 and a.uid=` + c.Uid)[0]["num"]
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["collect"] = collect
		js["focus"] = focus
		js["read"] = read
		js["order1"] = order1
		js["order2"] = order2
		js["order3"] = order3
		js["order4"] = order4
		js["shop"] = shop
		js["hideSame"] = 1
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["title"] = `我的`
		c.Data["info"] = info
		c.Data["collect"] = collect
		c.Data["focus"] = focus
		c.Data["read"] = read
		c.Data["order1"] = order1
		c.Data["order2"] = order2
		c.Data["order3"] = order3
		c.Data["order4"] = order4
		c.Data["shop"] = shop
		c.TplName = "wechat/my/index.html"
	}
}

type MyAddress struct {
	BaseController
}

func (c *MyAddress) Get() {
	address := models.Query(`select a.name,a.mobile,a.default,a.id,a.address,b.name as shen,c.name as shi,d.name as qu from mt_member_address a 
			left join mt_area b on a.province=b.code left join mt_area c on a.city=c.code left join mt_area d on a.area=d.code where 
			a.status=1 and a.uid=` + c.Uid + ` order by a.default desc,a.addtime desc`)
	if c.IsAjax() {
		c.Data["json"] = address
		c.ServeJSON()
	} else {
		c.Data["title"] = `我的收货地址`
		c.Data["info"] = address
		c.TplName = "wechat/my/address.html"
	}
}

func (c *MyAddress) Post() {
	defer c.ServeJSON()
	if c.Uid != "" {
		id := c.GetString("id")
		if err := models.Query("update mt_member_address set `default`=0 where uid=" + c.Uid); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
		} else {
			if err = models.Query("update mt_member_address set `default`=1 where id=" + id); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
			} else {
				c.Data["json"] = 1
			}
		}
	} else {
		c.Data["json"] = 0
	}
}

type MyAddressAE struct {
	BaseController
}

func (c *MyAddressAE) Get() {
	id := c.GetString("id")
	if id != "" {
		if info := models.Query(`select a.id,a.name,a.mobile,a.province,a.city,a.area,a.address,b.name as shen,c.name as shi,d.name as qu 
				from mt_member_address a left join mt_area b on a.province=b.code left join mt_area c on a.city=c.code left join mt_area d 
				on a.area=d.code where a.id=` + id + ` and a.uid=` + c.Uid); info != nil {
			if c.IsAjax() {
				var js = make(map[string]interface{})
				js["info"] = info[0]
				c.Data["json"] = js
				c.ServeJSON()
			} else {
				beego.Debug(info)
				c.Data["info"] = info[0]
				c.Data["title"] = `编辑收货地址`
				c.TplName = "wechat/my/address_ae.html"
			}
		} else {
			c.StopRun()
		}
	} else {
		if c.IsAjax() {
			var js = make(map[string]interface{})
			shen := models.Query(`select * from mt_area where pid=0`)
			for _, v := range shen {
				shi := models.Query(`select * from mt_area where pid=` + v["id"].(string))
				for _, s := range shi {
					s["qu"] = models.Query(`select * from mt_area where pid=` + s["id"].(string))
				}
				v["shi"] = shi
			}
			js["area"] = shen
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["title"] = `添加收货地址`
			c.TplName = "wechat/my/address_ae.html"
		}
	}
}

func (c *MyAddressAE) Post() {
	input := c.Input()
	id := input.Get("id")
	input.Set("uid", c.Uid)
	if id == "" {
		if isdefault := models.Query(`select id from mt_member_address where uid=` + c.Uid + ` and status=1`); isdefault == nil {
			input.Set("default", "1")
		}
		input.Set("addtime", sword.Gettimes(""))
	} else {
		if istrue := models.Query(`select id from mt_member_address where status=1 and uid=` + c.Uid + ` and id=` + id); istrue == nil {
			input.Set("id", "")
		}
	}
	if _, err := models.Save("mt_member_address", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type MyMoney struct {
	BaseController
}

func (c *MyMoney) Get() {
	info := models.Query(`select a.id,a.money,a.type from mt_member a where a.id=` + c.Uid)
	isshop := models.Query(`select id from mt_shop where status=1 and style=1 and censoring=1 and uid=` + c.Uid + ` limit 1`)
	if isshop == nil {
		info[0]["isshop"] = 0
	} else {
		info[0]["isshop"] = 1
	}
	if c.IsAjax() {
		c.Data["json"] = info[0]
		c.ServeJSON()
	} else {
		c.Data["info"] = info[0]
		c.TplName = "wechat/my/money.html"
	}
}

type MyMoneyRecharge struct {
	BaseController
}

func (c *MyMoneyRecharge) Get() {
	mem := models.Query(`select wx_openid_gzh,wx_openid_xapp from mt_member where id=` + c.Uid)[0]
	if c.IsAjax() {
		c.Data["json"] = mem
		c.ServeJSON()
	} else {
		c.Data["mem"] = mem
		c.TplName = "wechat/my/recharge.html"
	}
}
func (c *MyMoneyRecharge) Post() {
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	c.Data["json"] = &js
	js["status"] = 1
	t := c.GetString("type")
	money, err := c.GetFloat("money")
	if err != nil {
		beego.Debug(err)
		js["status"] = 0
		return
	}
	style, _ := c.GetInt("style")
	if t == "1" {
		if style == 0 {
			user_agent := c.Ctx.Request.Header.Get("User-Agent")
			iswx := strings.Index(user_agent, "MicroMessenger")
			if iswx == -1 {
				res := WxPay(c.Uid, "充值", c.Ctx.Input.IP(), "", money, 1, 1)
				uri := models.Urlc + "/mobile/my/money"
				uris := url.QueryEscape(uri)
				resurl := res["url"].(string) + "&redirect_url=" + uris
				js["style"] = 0
				js["status"] = 1
				js["res"] = resurl
			} else {
				style, _ := c.GetInt("style")
				if style == 2 {
					js["res"] = WxPay(c.Uid, "充值", c.Ctx.Input.IP(), "", money, 2, 1)
				} else {
					js["res"] = WxPay(c.Uid, "充值", c.Ctx.Input.IP(), "", money, 0, 1)
				}
				js["style"] = 1
			}
		} else {
			js["res"] = WxPay(c.Uid, "充值", c.Ctx.Input.IP(), "", money, style, 1)
		}
	} else {
		if res, err := Alipay(c.Uid, money, "", 1, style); err != nil {
			beego.Debug(err)
			js["status"] = 0
		} else {
			js["res"] = res
		}
	}
	// utils.Display("js", js)
}

type MyMoneyGetcash struct {
	BaseController
}

func (c *MyMoneyGetcash) Get() {
	info := models.Query(`select a.money,a.cashpassword,b.pathname from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + c.Uid)[0]
	bank := models.Query(`select a.id,a.name,b.pathname from mt_systems_configuration a left join mt_resources b on a.img=b.id where a.alias='getcash' and a.status=1`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["bank"] = bank
		js["info"] = info
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.Data["bank"] = bank
		c.TplName = "wechat/my/getcash.html"
	}
}

func (c *MyMoneyGetcash) Post() {
	defer c.ServeJSON()
	uid := c.Uid
	money := c.GetString("money")
	isok := models.Query(`select id,cashpassword from mt_member where money>=` + money + ` and id=` + uid)
	if isok == nil {
		c.Data["json"] = 2
		return
	}
	cashpassword := sword.ToMD5(c.GetString("cashpassword"))
	cashpassword2 := isok[0]["cashpassword"]
	if cashpassword != cashpassword2 {
		c.Data["json"] = 3
		return
	}
	isshop := models.Query(`select id from mt_shop where status=1 and censoring=1 and uid=` + c.Uid)
	if isshop != nil {
		money2 := sword.Sum(money, fmt.Sprint(models.Systems()["shopcash"]))
		if isenou := models.Query(`select id from mt_member where money>=` + money2 + ` and uid=` + c.Uid); isenou == nil {
			c.Data["json"] = 4
			return
		}
	}
	o := models.Orm()
	o.Begin()
	if _, err := o.Raw(`update mt_member set money=money-` + money + ` where id=` + uid).Exec(); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
		o.Rollback()
		return
	}
	sql := fmt.Sprintf(`insert into mt_member_getcash (uid,money,bank,account,name,addtime) values ('%v','%v','%v','%v','%v','%v')`,
		uid, money, c.GetString("bank"), c.GetString("account"), c.GetString("name"), sword.Gettimes(""))
	if _, err := o.Raw(sql).Exec(); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
		o.Rollback()
	} else {
		if _, err := o.Raw(fmt.Sprintf(`insert into mt_member_money_detail (uid,type,money,addtime,bank) values ('%v','%v','%v','%v','%v')`,
			uid, 1, money, sword.Gettimes(""), c.GetString("bank"))).Exec(); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
			o.Rollback()
		} else {
			o.Commit()
			c.Data["json"] = 1
			bank := models.Query(`select name from mt_systems_configuration where id=` + c.GetString("bank"))[0]["name"]
			openid := models.Query(`select b.wx_openid_gzh from mt_systems a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"]
			data2 := map[string]map[string]interface{}{
				"first":    {"value": `您有一个新的提现申请`},
				"keyword1": {"value": c.GetString("name")},
				"keyword2": {"value": sword.Gettimes(""), "color": "#9c9c9c"},
				"keyword3": {"value": bank},
				"keyword4": {"value": money, "color": "#e4393c"},
				"remark":   {"value": `请您尽快处理~`},
			}
			models.Push(fmt.Sprint(openid), `JFrrx9CxZwepPyYz6VdwLRFa1e5xFbof3-DEGhtoEsI`, data2, "", "")
		}
	}

}

type MyOrder struct {
	BaseController
}

func (c *MyOrder) Get() {
	var w []string
	w = append(w, `a.uid=`+c.Uid)
	status := c.GetString("status")
	statusajax := c.GetString("statusajax")
	if statusajax == "" {
		if status == "" {
			w = append(w, `a.status>0`)
		} else {
			w = append(w, `a.status=`+status)
		}
	} else {
		if statusajax == "0" {
			w = append(w, `a.status>0`)
		} else {
			w = append(w, `a.status=`+statusajax)
		}
	}
	page, _, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.gid,a.id,a.money,a.count,a.carid,a.status,a.type,a.goodtime,a.agid,b.name from mt_order a left join mt_shop b on a.gid=b.id
	 where ` + ws + ob + lt
	info := models.Query(sql)
	for _, v := range info {
		carid := fmt.Sprint(v["carid"])
		status := fmt.Sprint(v["status"])
		shopcar := models.Query(`select a.gid,a.pid,a.num,a.sendnum,a.status,b.title,b.spec,b.number,a.price,c.pathname,b.model,a.spec as spec2,
			b.width,b.length,b.height,b.weight 
		 from mt_order_car a left join mt_product b on a.pid=b.id 
			left join mt_resources c on b.img=c.id where a.id in(` + carid + `)`)
		var tj = "0.00"
		var zl = "0.00"
		var sl = 0
		for _, v := range shopcar {
			width := fmt.Sprint(v["width"])
			length := fmt.Sprint(v["length"])
			height := fmt.Sprint(v["height"])
			weight := fmt.Sprint(v["weight"])
			number := fmt.Sprint(v["number"])
			num := fmt.Sprint(v["num"])
			numi, _ := strconv.Atoi(num)
			tjone := sword.Divi(sword.Mult(width, sword.Mult(height, sword.Mult(length, num))), "1000000")
			tj = sword.Sum(tj, tjone)
			sl += numi
			zlone := sword.Mult(sword.Mult(weight, number), num)
			zl = sword.Sum(zl, zlone)
		}
		v["tj"] = tj
		v["zl"] = sword.Divi(zl, "1000")
		v["sl"] = sl
		v["shopcar"] = shopcar
		var isre = 0
		goodtime := fmt.Sprint(v["goodtime"])
		if status == "4" || status == "5" {
			var t int
			freight, _ := strconv.Atoi(fmt.Sprint(models.Systems()["freight"]))
			exile, _ := strconv.Atoi(fmt.Sprint(models.Systems()["exile"]))
			if freight > exile {
				t = freight
			} else {
				t = exile
			}
			timenow := time.Now().Local()
			loc, _ := time.LoadLocation("Local")
			timegood, _ := time.ParseInLocation(Tparse, goodtime, loc)
			timegood = timegood.AddDate(0, 0, t)
			if timenow.Before(timegood) {
				isre = 1
			}

		}
		v["isre"] = isre
		if status == "6" {
			v["return"] = models.Query("select * from mt_order_return where status=1 and oid=" + v["id"].(string))[0]
		}
	}
	mem := models.Query(`select paypassword as token,wx_openid_gzh,wx_openid_xapp,wx_openid_app from mt_member where id=` + c.Uid)[0]
	if mem["wx_openid_xapp"] == "<nil>" {
		mem["wx_openid_xapp"] = ""
	}
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["mem"] = mem
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.Data["status"] = status
		c.Data["mem"] = mem
		c.TplName = "wechat/my/order.html"
	}
}
func (c *MyOrder) Post() {
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	c.Data["json"] = &js
	oid := c.GetString("oid")
	paytype := c.GetString("paytype")
	js["type"] = paytype
	// if paytype == "1" {
	// 	password := c.GetString("password")
	// 	if password != "" {
	// 		paypassword := sword.ToMD5(password)
	// 		if ispok := models.Query(`select id from mt_member where paypassword='` + paypassword + `' and id=` + c.Uid); ispok == nil {
	// 			js["status"] = 3
	// 			return
	// 		}
	// 	}
	// }
	if order := models.Query(`select ordernum,gid,money,addtime,carid from mt_order where uid=` + c.Uid + ` and id=` + oid + ` and status=1`); order != nil && paytype != "" {
		money := order[0]["money"].(string)
		nowtime := sword.Gettimes("")
		style, _ := c.GetInt("style")
		switch paytype {
		case "1":
			moneyen := models.Query(`select id from mt_member where money>=` + money + ` and id=` + c.Uid)
			if moneyen == nil {
				js["status"] = 2 //余额不足
				return
			}
			o := models.Orm()
			o.Begin()
			if _, err := o.Raw(`update mt_member set money=money-` + money + ` where id=` + c.Uid).Exec(); err != nil {
				beego.Debug(err)
				js["status"] = 0
				o.Rollback()
				return
			}
			if _, err := o.Raw(fmt.Sprintf(`update mt_order set status=2,paytime='%v' where id=%v`, nowtime, oid)).Exec(); err != nil {
				beego.Debug(err)
				js["status"] = 0
				o.Rollback()
				return
			}
			if _, err := o.Raw(fmt.Sprintf(`insert into mt_member_money_detail (uid,type,money,oid,addtime,paytype) values 
							('%v','%v','%v','%v','%v','%v')`, c.Uid, 3, money, oid, nowtime, paytype)).Exec(); err != nil {
				beego.Debug(err)
				js["status"] = 0
				o.Rollback()
				return
			}
			js["status"] = 1
			o.Commit()
			PayPush(oid)
		case "2":
			moneyf, err := strconv.ParseFloat(money, 64)
			if style == 1 {
				res := WxPay(c.Uid, "支付订单", c.Ctx.Input.IP(), oid, moneyf, 1, 0)
				uri := models.Urlc + "/mobile/my/order?status=2"
				uris := url.QueryEscape(uri)
				resurl := res["url"].(string) + "&redirect_url=" + uris
				js["style"] = 1
				js["status"] = 1
				js["res"] = resurl
			} else {
				if err != nil {
					js["status"] = 0
				} else {
					js["style"] = 0
					js["status"] = 1
					js["res"] = WxPay(c.Uid, "支付订单", c.Ctx.Input.IP(), oid, moneyf, style, 0)
				}
			}
		case "3":
			moneyf, err := strconv.ParseFloat(money, 64)
			if err != nil {
				js["status"] = 0
			} else {
				if res, err := Alipay(c.Uid, moneyf, oid, 0, style); err != nil {
					beego.Debug(err)
					js["status"] = 0
				} else {
					js["status"] = 1
					js["res"] = res
				}
			}
		}
	} else {
		js["status"] = 0
	}
}

type MyOrderDetail struct {
	BaseController
}

func (c *MyOrderDetail) Get() {
	id := c.GetString("id")
	beego.Debug(c.Input())
	if id == "" {
		return
	}
	order := models.Query(`select a.*,b.pathname,z.name as shen,x.name as shi,c.name as qu,e.offer,g.name as gname,g.tel from mt_order a 
		left join mt_resources b on a.sendimg=b.id left join mt_member_coupon d on a.yid=d.id left join mt_coupon e on d.yid=e.id 
		left join mt_area z on a.province=z.code left join mt_area x on a.city=x.code left join mt_area c on a.area=c.code left join 
		mt_shop g on a.gid=g.id where a.id=` + id)[0]
	carid := fmt.Sprint(order["carid"])
	if carid == "" || carid == "<nil>" {
		carid = "''"
	}
	var shopcar []orm.Params
	shopcar = models.Query(`select a.pid,a.num,a.sendnum,a.status,b.title,b.spec,b.number,a.price,c.pathname,b.model,b.cid,a.uid,a.spec as spec2 from mt_order_car a 
			left join mt_product b on a.pid=b.id left join mt_resources c on b.img=c.id where a.id in(` + carid + `)`)
	var cids []string
	for _, v := range shopcar {
		cids = append(cids, v["cid"].(string))
	}
	cid := strings.Join(cids, ",")
	recommend := ProductList(`select a.id,a.title,a.price,a.mprice,b.pathname,a.isvip from mt_product a left join mt_resources b on a.img=b.id where a.cid in(`+cid+`) 
		and a.status=1 and a.censoring=1 order by a.recommend desc,a.sort desc,a.addtime desc limit 10`, c.Uid)
	order["shopcar"] = shopcar
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["order"] = order
		js["recommend"] = recommend
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["order"] = order
		c.Data["recommend"] = recommend
		c.TplName = "wechat/my/order_detail.html"
	}
}

type MyOrderReturnApply struct {
	BaseController
}

func (c *MyOrderReturnApply) Get() {
	rid := c.GetString("rid")
	var id string
	var rinfo orm.Params
	if rid != "" {
		rinfo = models.Query(`select a.id,a.oid,a.why,b.name as wname,a.intr from mt_order_return a left join mt_systems_configuration b on a.why=b.id 
			where a.id=` + rid)[0]
		id = rinfo["oid"].(string)
	} else {
		id = c.GetString("id")
	}
	if order := models.Query(`select a.gid,a.id,a.money,a.count,a.carid,a.status,a.agid,a.type,a.goodtime,b.name as gname from mt_order a left join mt_shop b on a.gid=b.id 
			where a.status in(2,4,5) and a.id=` + id); order != nil {
		carid := fmt.Sprint(order[0]["carid"])
		shopcar := models.Query(`select a.pid,a.num,a.sendnum,a.status,a.uid,b.title,b.spec,b.number,a.price,c.pathname,b.model,a.spec as spec2 from mt_order_car a left join mt_product b on a.pid=b.id 
			left join mt_resources c on b.img=c.id where a.id in(` + carid + `)`)
		order[0]["shopcar"] = shopcar
		why := models.Query(`select id,name from mt_systems_configuration where status=1 and alias='why'`)
		status := fmt.Sprint(order[0]["status"])
		goodtime := fmt.Sprint(order[0]["goodtime"])
		var isre = 0
		//0 失效
		//1 退换货都支持
		//2 仅退货
		//3 仅换货
		if status == "4" {
			isre = 1
		} else if status == "5" {
			freight, _ := strconv.Atoi(fmt.Sprint(models.Systems()["freight"])) //换货期限
			exile, _ := strconv.Atoi(fmt.Sprint(models.Systems()["exile"]))     //退货期限
			timenow := time.Now().Local()
			loc, _ := time.LoadLocation("Local")
			timegood, _ := time.ParseInLocation(Tparse, goodtime, loc)
			timefreight := timegood.AddDate(0, 0, freight)
			timeexile := timegood.AddDate(0, 0, exile)
			if timenow.Before(timefreight) && timenow.Before(timeexile) {
				isre = 1
			} else if !timenow.Before(timefreight) && timenow.Before(timeexile) {
				isre = 2
			} else if timenow.Before(timefreight) && !timenow.Before(timeexile) {
				isre = 3
			}
		} else if status == "2" {
			isre = 2
		}
		beego.Debug(isre)
		if isre == 0 {
			return
		}
		order[0]["isre"] = isre
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = order[0]
			js["why"] = why
			js["rinfo"] = rinfo
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = order[0]
			c.Data["why"] = why
			c.Data["rinfo"] = rinfo
			c.TplName = "wechat/my/order_return_apply.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my/order")
		}
	}
}

func (c *MyOrderReturnApply) Post() {
	defer c.ServeJSON()
	input := c.Input()
	order := models.Query(`select a.gid,a.addtime,a.status,a.money,a.ordernum,b.monicker,a.carid from mt_order a left join mt_member 
		b on a.uid=b.id where a.id=` + input.Get("oid"))[0]
	monicker := fmt.Sprint(order["monicker"])
	input.Del("id")
	input.Set("addtime", sword.Gettimes(""))
	input.Set("check", "0")
	input.Set("agree", "0")
	input.Set("id", c.GetString("rid"))
	input.Del("rid")
	shopcar := c.GetString("shopcar")
	beego.Debug(shopcar)
	shopcars := strings.Split(shopcar, ",")
	carid := fmt.Sprint(order["carid"])
	carids := strings.Split(carid, ",")
	for k, v := range carids {
		if err := models.Exec(`update mt_order_car set rnum=` + shopcars[k] + ` where id=` + v); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
			return
		}
	}
	input.Del("shopcar")
	var first string
	if c.GetString("rid") == "" {
		input.Set("addtime", sword.Gettimes(""))
		first = monicker + `向您发起了一笔退款申请,请及时处理`
	} else {
		first = monicker + `重新提交了退款申请,请及时处理`
	}
	if _, err := models.Save("mt_order_return", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
		return
	} else {
		c.Data["json"] = 1
		if err = models.Exec(`update mt_order set status=6 where id=` + input.Get("oid")); err != nil {
			beego.Debug(err)
		}
	}
	gopenid := fmt.Sprint(models.Query(`select b.wx_openid_gzh from mt_systems a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"])
	data1 := map[string]map[string]interface{}{
		"first":    {"value": first},
		"keyword1": {"value": order["ordernum"], "color": `#36353A`},
		"keyword2": {"value": order["money"], "color": `#e31939`},
		"keyword3": {"value": order["addtime"], "color": "#36353A"},
		"remark":   {"value": `您可以进入后台城查看~`},
	}
	models.Push(gopenid, `L-BI2hpqu5MJ5sI0Rm8-j1jeTMmGAnU_oEYphIUPvCE`, data1, models.Urlc+"/mobile/index", "")

}

type MyOrderReturnDetail struct {
	BaseController
}

func (c *MyOrderReturnDetail) Get() {
	id := c.GetString("id")
	if re := models.Query(`select a.*,b.name as whys,c.ordernum,c.carid,c.agid,c.type as ctype,d.tel,c.money from mt_order_return a left join mt_systems_configuration b on a.why=b.id
		 left join mt_order c on a.oid=c.id left join mt_shop d on c.gid=d.id where a.status=1 and a.id=` + id); re != nil {
		for _, v := range re {
			v["gid"] = models.Query(`select b.gid from mt_order_car a join mt_product b on a.pid=b.id where a.id in(` + fmt.Sprint(v["carid"]) + `)`)[0]["gid"]
			product := models.Query(`select b.model,a.num,a.sendnum,a.status,b.id,a.price,b.title,b.number,b.spec,c.pathname from mt_order_car a left join mt_product b on a.pid=b.id left join mt_resources c 
		 			on b.img=c.id where a.id in(` + fmt.Sprint(v["carid"]) + `)`)
			v["product"] = product
		}
		if c.IsAjax() {
			// var js = make(map[string]interface{})
			// js["info"] = order[0]
			// js["why"] = why
			c.Data["json"] = re[0]
			c.ServeJSON()
		} else {
			c.Data["info"] = re[0]
			c.TplName = "wechat/my/order_return_detail.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my")
		}
	}
}
func (c *MyOrderReturnDetail) Post() {
	id := c.GetString("id")
	if err := models.Exec(`update mt_order_return set status=0 where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		re := models.Query(`select oid from mt_order_return where id=` + id)[0]
		oid := re["oid"].(string)
		if err := models.Exec(`update mt_order set status=5 where id=` + oid); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
		} else {
			c.Data["json"] = 1
		}
	}
	c.ServeJSON()
}

type MyFoot struct {
	BaseController
}

func (c *MyFoot) Get() {
	var w []string
	w = append(w, `a.uid=`+c.Uid)
	w = append(w, `a.pid>0`)
	w = append(w, `a.status=1`)
	page, _, num := models.Pagechange(c.GetString("page"), "")
	ob := ` order by a.addtime desc`
	lt := ` limit ` + page + `,` + num
	ws := strings.Join(w, ` and `)
	sql := `select a.pid as id,b.title,a.pid,c.pathname,b.price,a.addtime,b.isvip from mt_systems_reading a join mt_product b on a.pid=b.id 
		left join mt_resources c on b.img=c.id where ` + ws + ob + lt
	info := ProductList(sql, c.Uid)
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/my/foot.html"
	}
}

type MyCF struct {
	BaseController
}

func (c *MyCF) Get() {
	var w []string
	w = append(w, `a.uid=`+c.Uid)
	ob := ` order by a.addtime desc`
	// page, _, num := models.Pagechange(c.GetString("page"), "")
	// lt := ` limit ` + page + `,` + num
	ws := strings.Join(w, ` and `)
	sql := `select a.id as cid,b.id,b.title,b.cid,a.pid,b.gid,c.pathname,b.price,b.isvip,b.issellnum,b.sellnum,b.keywords,d.name as gname,e.name as shen from 
		mt_member_collection a join (select * from mt_product where censoring=1 and status=1) b on a.pid=b.id left join mt_resources c on b.img=c.id 
		left join mt_shop d on b.gid=d.id left join mt_area e on d.province=e.id where ` + ws + ob
	info := ProductList(sql, c.Uid)
	var cids []string
	for _, v := range info {
		cids = append(cids, fmt.Sprint(v["cid"]))
	}
	var w2 []string
	w2 = append(w2, `a.status=1`)
	w2 = append(w2, `a.censoring=1`)
	cidss := strings.Join(cids, ",")
	if cidss != "" {
		w2 = append(w2, `a.cid in(`+cidss+`)`)
	}
	w2s := strings.Join(w2, ` and `)
	productp := ProductList(`select a.id,a.title,a.mprice,a.price,b.pathname,a.isvip from mt_product a left join mt_resources b on a.img=b.id 
			where `+w2s+` order by a.recommend desc limit 4`, c.Uid)
	sql2 := `select a.id,a.gid from mt_member_focus a where ` + ws + ob
	info2 := models.Query(sql2)
	for _, v := range info2 {
		gid := v["gid"].(string)
		if gid == "0" {
			sys := models.Systems()
			v["pathname"] = sys["pathname"]
			v["name"] = sys["name"]
		} else {
			sh := models.Query(`select a.name,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where a.id=` + gid)[0]
			v["pathname"] = sh["pathname"]
			v["name"] = sh["name"]
		}
		v["num"] = models.Query(`select count(a.id) as num from mt_member_focus a where gid=` + gid)[0]["num"]
	}
	shopp := models.Query(`select a.id,a.name,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where a.status=1 and censoring=1 
			order by a.recommend desc,a.addtime desc limit 1`)
	for _, v := range shopp {
		product := models.Query(`select a.id,b.pathname,a.sellnum,a.issellnum from mt_product a left join mt_resources b on a.img=b.id where 
				a.censoring=1 and a.status=1 and a.gid=` + v["id"].(string) + ` order by a.grecommend desc,a.gsort desc`)
		pnum := len(product)
		v["pnum"] = pnum
		if pnum >= 3 {
			v["product"] = product[0:3]
		} else {
			v["product"] = product
		}
		var sellnum int
		for _, p := range product {
			var snum int
			if fmt.Sprint(p["issellnum"]) == "1" {
				sellnum := models.Query(`select sum(a.num) as num from mt_order_car a where a.status>1 and a.pid=` + v["id"].(string))[0]["num"]
				if sellnums, ok := sellnum.(string); ok {
					snum, _ = strconv.Atoi(sellnums)
				} else {
					snum = 0
				}
			} else {
				snum, _ = strconv.Atoi(p["sellnum"].(string))
			}
			sellnum += snum
		}
		v["sellnum"] = sellnum
	}
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["shopp"] = shopp
		js["productp"] = productp
		js["collect"] = info
		js["focus"] = info2
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["type"] = strings.Split(c.Ctx.Request.RequestURI, "/")[3]
		c.Data["shopp"] = shopp
		c.Data["productp"] = productp
		c.Data["collect"] = info
		c.Data["focus"] = info2
		c.TplName = "wechat/my/cf.html"
	}
}

func (c *MyCF) Post() {
	id := c.GetString("id")
	table := c.GetString("table")
	var js = make(map[string]interface{})

	if istrue := models.Query(`select id from ` + table + ` where id=` + id + ` and uid=` + c.Uid); istrue != nil {
		if err := models.Exec(`delete from ` + table + ` where id=` + id); err != nil {
			beego.Debug(err)
			js["status"] = 0
		} else {
			js["status"] = 1
			js["num"] = models.Query(`select count(*) as num from ` + table + ` where uid=` + c.Uid)[0]["num"]
		}
	} else {
		js["status"] = 0
	}
	c.Data["json"] = js
	c.ServeJSON()
}

type MyCoupon struct {
	BaseController
}

func (c *MyCoupon) Get() {
	var w []string
	w = append(w, `a.uid=`+c.Uid)
	w = append(w, `a.status=1`)
	w = append(w, `b.endtime>'`+sword.Gettimes("")+`'`)
	ob := ` order by a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.id,b.name,b.offer,b.full,date_format(b.starttime,'%Y-%m-%d') as starttime,date_format(b.endtime,'%Y-%m-%d') as endtime,a.addtime,
	c.name as gname,d.pathname,b.gid from 
		mt_member_coupon a left join mt_coupon b on a.yid=b.id left join mt_shop c on b.gid=c.id 
		left join mt_resources d on b.img=d.id where ` + ws + ob
	info := models.Query(sql)
	if c.IsAjax() {
		// var js = make(map[string]interface{})
		// js["shopp"] = shopp
		// js["productp"] = productp
		// js["collect"] = info
		// js["focus"] = info2
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/my/coupon.html"
	}
}

type MyShop struct {
	BaseController
}

func (c *MyShop) Get() {
	if isp := models.Query(`select bid from mt_shop where censoring=1 and status=1 and style=0 and uid=` + c.Uid); isp != nil {
		var sids []string
		for _, v := range isp {
			sids = append(sids, fmt.Sprint(v["bid"]))
		}
		sid := strings.Join(sids, ",")
		var w []string
		w = append(w, `a.status=2`)
		ws := strings.Join(w, ` and `)
		page, _, num := models.Pagechange(c.GetString("page"), "")
		lt := ` limit ` + page + `,` + num
		ob := ` order by a.paytime desc`
		sql := `select distinct a.id,a.carid,a.ordernum from mt_order a join (select a.id from mt_order_car a join (select * from mt_product where bid in(` + sid + `) and censoring=1 and status=1) b where a.pid=b.id and a.status>1) b where find_in_set(b.id,a.carid)>0 and ` + ws + ob + lt
		info := models.Query(sql)
		for _, i := range info {
			carid := fmt.Sprint(i["carid"])
			shopcar := models.Query(`select a.num,a.status,a.id,a.sendnum,a.pid,b.title,b.length,b.width,b.height,b.weight,b.number,b.spec,
				b.isw,d.monicker,c.pathname,e.pathname as path from mt_order_car a join (select * from mt_product where bid in(` + sid + `))
				 b on a.pid=b.id left join mt_member d on a.did=d.id left join mt_resources c on d.avatar=c.id left join mt_resources e 
				 on b.img=e.id where a.id in(` + carid + `) order by a.paytime`)
			var we = "0.00"
			var volume = "0.00"
			for _, v := range shopcar {
				isw := fmt.Sprint(v["isw"])
				width := fmt.Sprint(v["width"])
				height := fmt.Sprint(v["height"])
				length := fmt.Sprint(v["length"])
				number := fmt.Sprint(v["number"])
				num := fmt.Sprint(v["num"])
				weight := fmt.Sprint(v["weight"])
				weight = sword.Divi(sword.Mult(sword.Mult(weight, number), num), "1000")
				if isw == "0" {
					volume = sword.Sum(volume, sword.Mult(length, sword.Mult(height, width)))
				} else {
					we = sword.Sum(we, weight)
				}
			}
			i["we"] = we
			i["volume"] = sword.Divi(volume, "1000000")
			i["shopcar"] = shopcar
		}
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = info
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = info
			c.TplName = "wechat/my/shop.html"
		}
	} else {
		c.StopRun()
	}
}

func (c *MyShop) Post() {
	defer c.ServeJSON()
	id := c.GetString("id")
	oid := c.GetString("oid")
	num := c.GetString("num")
	openid := models.Query(`select c.wx_openid_gzh,a.ordernum from mt_order a left join mt_member b on a.uid=b.id
		 left join mt_member c on b.sid=c.id where a.id=` + oid)[0]
	if num == "" {
		if err := models.Exec(`update mt_order_car set sendnum=num,status=3 where id=` + id); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
		} else {
			c.Data["json"] = 1
		}
	} else {
		if is := models.Query(`select id from mt_order_car where num>=` + num + ` and id=` + id); is != nil {
			if err := models.Exec(`update mt_order_car set sendnum=num-` + num + `,status=3 where id=` + id); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
			} else {
				c.Data["json"] = 1
			}
			openids := fmt.Sprint(openid["wx_openid_gzh"])
			product := models.Query(`select b.title from mt_order_car a left join mt_product b on a.pid=b.id where a.id=` + id)[0]["title"].(string)
			data3 := map[string]map[string]interface{}{
				"first":    {"value": `有笔订单缺货提醒`},
				"keyword1": {"value": openid["ordernum"], "color": `#36353A`},
				"keyword2": {"value": sword.Gettimes(""), "color": "#9c9c9c"},
				"keyword3": {"value": product + ` x ` + num, "color": `#e31939`},
				"remark":   {"value": `请您尽快联系客户,说明情况`},
			}
			models.Push(openids, `HDTqyPEaJhm9GJYf6sdLPEoWGx9F5bRQNEH6uBTL0Dg`, data3, models.Urlc+"/mobile/index", "")
		} else {
			c.Data["json"] = 2
		}
	}
	if c.Data["json"].(int) == 1 {
		//提货推送
		shop := models.Query(`select c.*,b.title,a.sendnum,a.did from mt_order_car a join mt_product b on a.pid=b.id join mt_shop c on b.gid=c.id where a.id=` + id)
		if shop == nil {
			//待分配
			return
		}
		if fmt.Sprint(shop[0]["did"]) == "" || fmt.Sprint(shop[0]["did"]) == "0" || fmt.Sprint(shop[0]["did"]) == "<nil>" {
			area := fmt.Sprint(shop[0]["area"])
			driver := models.Query(`select id,wx_openid_gzh,concat(',',driverarea,',') as driverarea 
				from mt_member where driver=1 and status=1 and driverarea like '%,` + area + `,%'`)
			if driver != nil {
				if err := models.Exec(`update mt_order_car set did=` + driver[0]["id"].(string)); err != nil {
					beego.Debug(err)
					return
				}
				data4 := map[string]map[string]interface{}{
					"first":    {"value": fmt.Sprintf(`您需要提取 %v x %v`, shop[0]["title"], shop[0]["sendnum"])},
					"keyword1": {"value": openid["ordernum"], "color": `#36353A`},
					"keyword2": {"value": shop[0]["name"], "color": "#e31939"},
					"keyword3": {"value": shop[0]["address"], "color": `#333`},
					"keyword4": {"value": sword.Gettimes(""), "color": `#9c9c9c`},
					"remark":   {"value": `请您尽快前往提货`},
				}
				models.Push(fmt.Sprint(driver[0]["wx_openid_gzh"]), `erYqlXpwON3LAZ7oVmwWgV27mDQCYJbsuxSpkevQyVA`, data4, models.Urlc+"/mobile/my", "")
			}
		}
	}
}

type MyDriver struct {
	BaseController
}

func (c *MyDriver) Get() {
	if isp := models.Query(`select id from mt_member where driver=1 and status=1 and id=` + c.Uid); isp != nil {
		var w []string
		w = append(w, `c.status>1`)
		ws := strings.Join(w, ` and `)
		page, _, num := models.Pagechange(c.GetString("page"), "")
		lt := ` limit ` + page + `,` + num
		ob := ` order by c.paytime desc`
		sql := `select c.id,c.carid,c.ordernum from mt_order c join (select * from mt_order_car where did=` + c.Uid + `) a on a.id in(c.carid) join mt_product 
		b on a.pid=b.id where ` + ws + ob + lt
		info := models.Query(sql)
		for _, i := range info {
			carid := fmt.Sprint(i["carid"])
			shopcar := models.Query(`select a.num,a.status,a.id,a.sendnum,a.pid,b.title,b.length,b.width,b.height,b.weight,b.number,b.spec,
				b.isw,d.name,c.pathname,e.pathname as path,d.address,d.tel from mt_order_car a join mt_product b on a.pid=b.id left join 
				mt_shop d on b.gid=d.id left join mt_resources c on d.logo=c.id left join mt_resources e
				 on b.img=e.id where a.id in(` + carid + `) and a.did=` + c.Uid + ` order by a.paytime`)
			var we = "0.00"
			var volume = "0.00"
			for _, v := range shopcar {
				isw := fmt.Sprint(v["isw"])
				width := fmt.Sprint(v["width"])
				height := fmt.Sprint(v["height"])
				length := fmt.Sprint(v["length"])
				weight := fmt.Sprint(v["weight"])
				if isw == "0" {
					volume = sword.Sum(volume, sword.Mult(length, sword.Mult(height, width)))
				} else {
					we = sword.Sum(we, weight)
				}
			}
			i["we"] = we
			i["volume"] = sword.Divi(volume, "1000000")
			i["shopcar"] = shopcar
		}
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = info
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = info
			c.TplName = "wechat/my/driver.html"
		}
	} else {
		c.StopRun()
	}
}
func (c *MyDriver) Post() {
	defer c.ServeJSON()
	id := c.GetString("id")
	if err := models.Exec(`update mt_order_car set status=4 where id=` + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
}

type MyOrderConfirm struct {
	BaseController
}

func (c *MyOrderConfirm) Get() {
	defer c.ServeJSON()
	id := c.GetString("id")
	istrue := models.Query(`select id from mt_order where status=3 and uid=` + c.Uid + ` and id=` + id)
	if istrue == nil {
		c.Data["json"] = 0
		return
	}
	if err := OrderConfirmF(id); err != nil {
		c.Data["json"] = 0
		beego.Debug(err)
		return
	}
	c.Data["json"] = 1
}

type MyOrderEvaluationAdd struct {
	BaseController
}

func (c *MyOrderEvaluationAdd) Get() {
	id := c.GetString("id")
	if istrue := models.Query(`select id,carid from mt_order where status=4 and uid=` + c.Uid + ` and id=` + id); istrue != nil {
		carid := fmt.Sprint(istrue[0]["carid"])
		shopcar := models.Query(`select a.id,b.title,c.pathname from mt_order_car a left join mt_product b on a.pid=b.id left join mt_resources c 
				on b.img=c.id where a.id in(` + carid + `)`)
		if c.IsAjax() {
			c.Data["json"] = shopcar
			c.ServeJSON()
		} else {
			c.Data["shopcar"] = shopcar
			c.TplName = "wechat/my/evaluate_add.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my")
		}
	}
}

func (c *MyOrderEvaluationAdd) Post() {
	defer c.ServeJSON()
	id := c.GetString("id")
	if istrue := models.Query(`select id,gid from mt_order where status=4 and uid=` + c.Uid + ` and id=` + id); istrue != nil {
		style := c.GetString("style")
		score := c.GetStrings("score")
		cid := c.GetStrings("cid")
		content := c.GetStrings("content")
		img := c.GetStrings("img")
		if style == "2" {
			cid = strings.Split(c.GetString("cid"), ",")
			score = strings.Split(c.GetString("score"), ",")
			img = strings.Split(c.GetString("img"), ",")
			content = strings.Split(c.GetString("content"), ",")
		}
		shop_score := c.GetString("shop_score")
		shop_logisticsscore := c.GetString("shop_logisticsscore")
		// utils.Display("cid", cid)
		// utils.Display("img", img)
		// utils.Display("score", score)
		// utils.Display("content", content)
		o := models.Orm()
		o.Begin()
		nowtime := sword.Gettimes("")
		if _, err := o.Raw(fmt.Sprintf(`insert into mt_shop_evaluation (uid,gid,score,logisticsscore,addtime) values (%v,%v,%v,%v,'%v')`,
			c.Uid, 0, shop_score, shop_logisticsscore, nowtime)).Exec(); err != nil {
			beego.Debug(err)
			o.Rollback()
			c.Data["json"] = 0
			return
		}
		for k, v := range cid {
			imgs := strings.Trim(img[k], ",")
			sql := fmt.Sprintf(`insert into mt_product_evaluation (uid,cid,content,img,score,addtime) values (%v,%v,'%v','%v',%v,'%v')`,
				c.Uid, v, content[k], imgs, score[k], nowtime)
			if _, err := o.Raw(sql).Exec(); err != nil {
				beego.Debug(err)
				o.Rollback()
				c.Data["json"] = 0
				return
			}
		}
		if _, err := o.Raw(`update mt_order set status=5 where id=` + id).Exec(); err != nil {
			beego.Debug(err)
			o.Rollback()
			c.Data["json"] = 0
			return
		}
		o.Commit()
		c.Data["json"] = 1
	} else {
		c.Data["json"] = 2
	}
}

type MyMoneyList struct {
	BaseController
}

func (c *MyMoneyList) Get() {
	status := c.GetString("status")
	var w []string
	w = append(w, `a.uid=`+c.Uid)
	if status != "" {
		w = append(w, `a.type=`+status)
	}
	page, _, num := models.Pagechange(c.GetString("page"), "10")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.addtime desc`
	ws := strings.Join(w, ` and `)
	sql := `select a.id,a.type,a.money,a.oid,a.addtime,a.paytype,b.name as bname,c.pathname as bpathname from mt_member_money_detail a 
	left join mt_systems_configuration b on a.bank=b.id left join mt_resources c on b.img=c.id where ` + ws + ob + lt
	info := models.Query(sql)
	for _, v := range info {
		t := fmt.Sprint(v["type"])
		oid := fmt.Sprint(v["oid"])
		paytype := fmt.Sprint(v["paytype"])
		switch t {
		case "0":
			switch paytype {
			case "2":
				v["name"] = "微信"
				v["pathname"] = "/static/wechat/img/wechatpay.png"
			case "3":
				v["name"] = "支付宝"
				v["pathname"] = "/static/wechat/img/zfb.png"
			}
		case "1":
			v["name"] = v["bname"]
			v["name"] = v["bpathname"]
		case "3":
			order := models.Query(`select a.ordernum,b.name,c.pathname from mt_order a left join mt_shop b on a.gid=b.id left join mt_resources c
			 on b.logo=c.id where a.id in(` + oid + `)`)
			for _, o := range order {
				if o["pathname"] == nil {
					o["pathname"] = models.Systems()["pathname"]
				}
			}
			v["pathname"] = order[0]["pathname"]
			v["product"] = order
		case "2", "4", "5":
			order := models.Query(`select b.monicker,c.pathname from mt_order a left join mt_member b on a.uid=b.id left join mt_resources c on b.avatar=c.id where a.id in(` + oid + `)`)
			v["pathname"] = order[0]["pathname"]
			v["name"] = order[0]["monicker"]
		case "6":
			order := models.Query(`select a.ordernum from mt_order a where a.id=` + oid)
			v["name"] = order[0]["ordernum"]
			v["pathname"] = `/static/wechat/img/logo.png`
		case "7":
			order := models.Query(`select a.ordernum,c.pathname from mt_order a left join mt_member b on a.uid=b.id left join 
			mt_resources c on b.avatar=c.id where a.id=` + oid)
			v["name"] = order[0]["ordernum"]
			v["pathname"] = order[0]["pathname"]
		case "8":
			v["pathname"] = "/static/wechat/img/index_menu/hb.png"
		}
	}
	money := models.Query(`select money from mt_member where id=` + c.Uid)[0]["money"]
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["money"] = money
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.Data["money"] = money
		c.TplName = "wechat/my/money_list.html"
	}
}

type MyMessage struct {
	BaseController
}

func (c *MyMessage) Get() {
	uid := c.Uid
	sql := `select * from mt_sms where id in (select max(a.id) from mt_sms a where (fid=` + uid + ` or uid=` + uid + `) and uid<>0 and fid<>0 and 
	status=1 group by (a.fid+a.uid)) order by addtime desc`
	info := models.Query(sql)
	for _, v := range info {
		fid := v["fid"].(string)
		sid := v["uid"].(string)
		if uid == fid {
			v["member"] = models.Query(`select a.monicker,b.pathname,a.id from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + sid)[0]
		} else {
			v["member"] = models.Query(`select a.monicker,b.pathname,a.id from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + fid)[0]
		}
		fidi, _ := strconv.Atoi(fid)
		sidi, _ := strconv.Atoi(sid)
		idsum := fidi + sidi
		v["noread"] = models.Query(fmt.Sprintf(`select count(*) as num from mt_sms where uid+fid=%v and uid=%v and state=0 and status=1`, idsum, uid))[0]["num"]
	}
	c.Data["json"] = info
	if c.IsAjax() {
		c.ServeJSON()
	} else {
		c.TplName = "wechat/my/message.html"
	}
}

type MyMessageContent struct {
	BaseController
}

func (c *MyMessageContent) Get() {
	page, _, num := models.Pagechange(c.GetString("page"), "10")
	uid := c.Uid

	id := c.GetString("id")
	lt := ` limit ` + page + `,` + num
	sql := `select a.* from (select * from mt_sms where (uid=` + id + ` and fid=` + uid + `) or
		 (uid=` + uid + ` and fid=` + id + `) order by id desc ` + lt + `) a order by a.id`
	info := models.Query(sql)
	first := models.Query(`select a.monicker,a.id,b.pathname from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + uid)[0]
	second := models.Query(`select a.monicker,a.id,b.pathname from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + id)[0]
	if err := models.Exec(fmt.Sprintf("update mt_sms set state=1 where uid=%v and fid+uid=%v", uid, sword.Sum(id, uid))); err != nil {
		beego.Debug("打开聊天详情设为已读失败：", err)
	}
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["uid"] = uid
		js["info"] = info
		js["first"] = first
		js["second"] = second
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["uid"] = uid
		c.Data["first"] = first
		c.Data["second"] = second
		c.Data["json"] = info
		c.TplName = "wechat/my/message_content.html"
	}
}

type MyInfo struct {
	BaseController
}

func (c *MyInfo) Get() {
	info := models.Query(`select a.mobile,a.monicker,b.pathname,a.wx_openid_gzh,a.wx_openid_web,a.wx_openid_xapp,wx_unionid from mt_member a left join mt_resources b on a.avatar=b.id where a.id=` + c.Uid)[0]
	if info["wx_openid_xapp"] == "<nil>" {
		info["wx_openid_xapp"] = ""
	}
	mobile := fmt.Sprint(info["mobile"])
	mobileb := []byte(mobile)
	x := []byte("*")
	x1 := x[0]
	var mobilebs []byte
	for k, v := range mobileb {
		if k <= 2 || k >= 7 {
			mobilebs = append(mobilebs, v)
		} else {
			mobilebs = append(mobilebs, x1)
		}
	}
	info["mobile"] = string(mobilebs)
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/my/info.html"
	}
}

type MyInfoEdit struct {
	BaseController
}

func (c *MyInfoEdit) Get() {
	info := models.Query(`select a.sex,a.monicker,a.name,a.wechat,a.mail,b.pathname,a.avatar from mt_member a left join mt_resources b on a.avatar=b.id 
			where a.id=` + c.Uid)[0]
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/my/info_edit.html"
	}
}

func (c *MyInfoEdit) Post() {
	input := c.Input()
	input.Set("id", c.Uid)
	if _, err := models.Save(`mt_member`, input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type MyInfoSafe struct {
	BaseController
}

func (c *MyInfoSafe) Get() {
	info := models.Query(`select a.password,a.paypassword,a.cashpassword from mt_member a where a.id=` + c.Uid)[0]
	if c.IsAjax() {
		c.Data["json"] = info
		c.ServeJSON()
	} else {
		c.Data["info"] = info
		c.TplName = "wechat/my/info_safe.html"
	}
}

func (c *MyInfoSafe) Post() {
	input := c.Input()
	input.Set("id", c.Uid)
	if _, err := models.Save(`mt_member`, input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type MyInfoBindWechat struct {
	BaseController
}

func (c *MyInfoBindWechat) Get() {
	redirect_uri := url.QueryEscape(Url + "/mobile/bind_wechat_oauth")
	c.Ctx.Redirect(302, "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Wx_appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect")
}

type BindWechatOauth struct {
	BaseController
}

func (c *BindWechatOauth) Get() {
	code := c.Input().Get("code")
	token_url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Wx_appid + "&secret=" + Wx_secret + "&code=" + code + "&grant_type=authorization_code"
	req1 := httplib.Get(token_url)
	m1 := new(Token)
	req1.ToJSON(&m1)
	if m1.Errcode != "" {
		beego.Debug(m1.Errcode)
		c.StopRun()
	}
	access_token_url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + Wx_appid + "&grant_type=refresh_token&refresh_token=" + m1.Refresh_token
	req2 := httplib.Get(access_token_url)
	m2 := new(Token)
	req2.ToJSON(&m2)
	if m2.Errcode != "" {
		beego.Debug(m2.Errcode)
		c.StopRun()
	}
	user_info_url := "https://api.weixin.qq.com/sns/userinfo?access_token=" + m2.Access_token + "&openid=" + m2.Openid + "&lang=zh_CN"
	req3 := httplib.Get(user_info_url)
	m3 := new(UserInfo)
	req3.ToJSON(&m3)
	// str3, _ := req3.String()
	if m3.Errcode != "" {
		beego.Debug(m3.Errcode)
		c.StopRun()
	}
	if err := models.Exec(fmt.Sprintf(`update mt_member set wx_openid_gzh='%v',wx_unionid='%v' where id=%v`, m3.Openid, m3.Unionid, c.Uid)); err != nil {
		beego.Debug(err)
		c.StopRun()
	} else {
		c.Ctx.Redirect(302, "/mobile/my/info")
	}
}

type MyInfoSafeChangeP struct {
	BaseController
}

func (c *MyInfoSafeChangeP) Get() {
	if c.IsAjax() {
		c.Data["json"] = 1
		c.ServeJSON()
	} else {
		c.TplName = "wechat/my/info_safe_changep.html"
	}
}
func (c *MyInfoSafeChangeP) Post() {
	t := c.GetString("type")
	var pt string
	switch t {
	case "login":
		pt = "password"
	case "pay":
		pt = "paypassword"
	case "cash":
		pt = "cashpassword"
	default:
		c.StopRun()
	}
	info := models.Query(`select a.password,a.paypassword,a.cashpassword from mt_member a where a.id=` + c.Uid)[0]
	oldpwd := sword.ToMD5(c.GetString("oldpwd"))
	newpwd := sword.ToMD5(c.GetString("newpwd"))
	beego.Debug(c.GetString("oldpwd"))
	beego.Debug(c.GetString("newpwd"))
	if fmt.Sprint(info[pt]) == "" || fmt.Sprint(info[pt]) == "<nil>" {
		if err := models.Exec(`update mt_member set ` + pt + `='` + newpwd + `' where id=` + c.Uid); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
		} else {
			c.Data["json"] = 1
		}
	} else {
		if fmt.Sprint(info[pt]) == oldpwd {
			if err := models.Exec(`update mt_member set ` + pt + `='` + newpwd + `' where id=` + c.Uid); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
			} else {
				c.Data["json"] = 1
			}
		} else {
			c.Data["json"] = 2
		}
	}
	c.ServeJSON()
}

type MyPromotionApply struct {
	BaseController
}

func (c *MyPromotionApply) Get() {
	var style string
	if c.Ctx.Request.RequestURI == "/mobile/my/promotion_apply" {
		style = "1"
		c.Data["title"] = `推广申请`
	} else {
		c.Data["title"] = `分销申请`
		style = "2"
	}
	is := models.Query(`select a.id,a.type,a.style,a.card,a.cardimg,a.imgcard,a.name,a.mobile,a.content,b.pathname as pathname1,c.pathname as pathname2 
		from mt_promote_add a left join mt_resources b on a.cardimg=b.id left join mt_resources c on a.imgcard=c.id where 
		a.uid=` + c.Uid + ` and a.censoring=2 and a.style=` + style)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["style"] = style
		if is != nil {
			js["info"] = is[0]
		}
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		if is != nil {
			c.Data["json"] = is[0]
		}
		c.Data["style"] = style
		c.TplName = "wechat/my/promotion_apply.html"
	}
}
func (c *MyPromotionApply) Post() {
	defer c.ServeJSON()
	input := c.Input()
	input.Set("addtime", sword.Gettimes(""))
	input.Set("censoring", "0")
	input.Set("uid", c.Uid)
	id := c.GetString("id")
	if id == "" {
		if is := models.Query(`select id from mt_promote_add where status=1 and style=` + c.GetString("style") + ` and uid=` + c.Uid); is != nil {
			c.Data["json"] = 2
			return
		}
	}
	if _, err := models.Save("mt_promote_add", input); err != nil {
		c.Data["json"] = 0
		beego.Debug(err)
	} else {
		c.Data["json"] = 1
		openid := models.Query(`select b.wx_openid_gzh from mt_systems a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"].(string)
		data := map[string]map[string]interface{}{
			"first":    {"value": `您有一个新的推广或者分销申请`},
			"keyword1": {"value": c.GetString("name"), "color": "#e4393c"},
			"keyword2": {"value": sword.Gettimes(""), "color": "#9c9c9c"},
			"remark":   {"value": `请您尽快处理~`},
		}
		models.Push(openid, `pX5W6MyCs5k6qaxOKE6XBjiLoH_leCDDY63DbAbXJ2I`, data, "", "")
	}
}

type MyPromotio struct {
	BaseController
}

func (c *MyPromotio) Get() {
	if isp := models.Query(`select id,money,type from mt_member where type>0 and id=` + c.Uid); isp != nil {
		img, err := os.Open(`static/wxqr/` + c.Uid + `.jpg`)
		if err != nil {
			if err = models.GetQr(Url+`/mobile/index?share_uid=`+c.Uid, `static/wxqr/`+c.Uid+`.jpg`); err != nil {
				beego.Debug(err)
			}
		} else {
			img.Close()
		}
		pnum := models.Query(`select count(a.id) as num from mt_member a where a.uid=` + c.Uid)[0]["num"]
		pmoneys := models.Query(`select sum(a.money) as money from mt_member_money_detail a where a.type=4 and a.uid=` + c.Uid)[0]["money"]
		pmoney := ""
		if pmoneystring, ok := pmoneys.(string); ok {
			pmoney = pmoneystring
		} else {
			pmoney = "0.00"
		}
		page, _, num := models.Pagechange(c.GetString("page"), "")
		promotion := models.Query(`select a.money,DATE_FORMAT(a.addtime,'%Y-%m-%d') as addtime,b.money as ordermoney,c.monicker,d.pathname from mt_member_money_detail a left join mt_order b 
			on a.oid=b.id left join mt_member c on b.uid=c.id left join mt_resources d on c.avatar=d.id where a.uid=` + c.Uid + ` and (a.type=4 or a.type=2) 
			order by a.addtime desc limit ` + page + `,` + num)
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = isp[0]
			js["pnum"] = pnum
			js["pmoney"] = pmoney
			js["promotion"] = promotion
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = isp[0]
			c.Data["pnum"] = pnum
			c.Data["pmoney"] = pmoney
			c.Data["promotion"] = promotion
			c.TplName = "wechat/my/promotion.html"
		}
	} else {
		c.StopRun()
	}
}

type MyDistribution struct {
	BaseController
}

func (c *MyDistribution) Get() {
	if isp := models.Query(`select id,money from mt_member where type=2 and id=` + c.Uid); isp != nil {
		img, err := os.Open(`static/wxqr/` + c.Uid + `.jpg`)
		if err != nil {
			url := `http://qr.liantu.com/api.php?text=` + Url + `/mobile/index?share_uid=` + c.Uid
			resp := httplib.Get(url)
			resp.ToFile(`static/wxqr/` + c.Uid + `.jpg`)
		} else {
			img.Close()
		}
		pnum := models.Query(`select count(a.id) as num from mt_member a where a.uid=` + c.Uid)[0]["num"]
		pmoneys := models.Query(`select sum(a.money) as money from mt_member_money_detail a where a.type=2 and a.uid=` + c.Uid)[0]["money"]
		pmoney := ""
		if pmoneystring, ok := pmoneys.(string); ok {
			pmoney = pmoneystring
		} else {
			pmoney = "0.00"
		}
		promotion := models.Query(`select a.money,DATE_FORMAT(a.addtime,'%Y-%m-%d') as addtime,b.money as ordermoney,c.monicker,d.pathname from mt_member_money_detail a left join mt_order b 
			on a.oid=b.id left join mt_member c on b.uid=c.id left join mt_resources d on c.avatar=d.id where a.uid=` + c.Uid + ` and a.type=2 
			order by a.addtime desc limit 0,10`)
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = isp[0]["money"]
			js["pnum"] = pnum
			js["pmoney"] = pmoney
			js["promotion"] = promotion
			c.Data["json"] = js
		} else {
			c.Data["info"] = isp[0]
			c.Data["pnum"] = pnum
			c.Data["pmoney"] = pmoney
			c.Data["promotion"] = promotion
			c.TplName = "wechat/my/distribution.html"
		}
	} else {
		c.StopRun()
	}
}

type MyEvaluation struct {
	BaseController
}

func (c *MyEvaluation) Get() {
	var w []string
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.sid=0`)
	ws := strings.Join(w, ` and `)
	ob := ` order by a.addtime desc `
	page, _, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `,` + num
	sql := `select a.score,a.img,a.addtime,a.content,c.pathname,b.monicker,d.pid as id,e.title,e.price,e.isvip,f.pathname as ppath from mt_product_evaluation a left join mt_member b 
				on a.uid=b.id left join mt_resources c on b.avatar=c.id join (select * from mt_order_car where uid=` + c.Uid + `) d on a.cid=d.id left join mt_product e 
				on d.pid=e.id left join mt_resources f on e.img=f.id where ` + ws + ob + lt
	info := ProductList(sql, c.Uid)
	for _, v := range info {
		if img := fmt.Sprint(v["img"]); img != "" && img != "<nil>" {
			v["imgs"] = models.Query(`select pathname from mt_resources where id in(` + img + `)`)
		}
	}
	all := models.Query(`select count(a.id) as num from mt_product_evaluation a join (select * from mt_order_car where uid=` + c.Uid + `) d on 
				a.cid=d.id where ` + ws)[0]["num"]
	good := models.Query(`select count(a.id) as num from mt_product_evaluation a join (select * from mt_order_car where uid=` + c.Uid + `) d on 
				a.cid=d.id where a.score>3 and ` + ws)[0]["num"]
	middle := models.Query(`select count(a.id) as num from mt_product_evaluation a join (select * from mt_order_car where uid=` + c.Uid + `) d on 
				a.cid=d.id where a.score=3 and ` + ws)[0]["num"]
	nogood := models.Query(`select count(a.id) as num from mt_product_evaluation a join (select * from mt_order_car where uid=` + c.Uid + `) d on 
				a.cid=d.id where a.score<3 and ` + ws)[0]["num"]
	pic := models.Query(`select count(a.id) as num from mt_product_evaluation a join (select * from mt_order_car where uid=` + c.Uid + `) d on 
				a.cid=d.id where a.img<>'' and ` + ws)[0]["num"]
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["all"] = all
		js["good"] = good
		js["middle"] = middle
		js["nogood"] = nogood
		js["pic"] = pic
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["all"] = all
		c.Data["good"] = good
		c.Data["middle"] = middle
		c.Data["nogood"] = nogood
		c.Data["pic"] = pic
		c.Data["info"] = info
		c.TplName = "wechat/my/evaluate.html"
	}
}

type MyOrderManage struct {
	BaseController
}

func (c *MyOrderManage) Get() {
	var gid []string
	if c.Uid == fmt.Sprint(models.Systems()["uid"]) {
		gid = append(gid, "0")
	}
	if g := models.Query(`select id from mt_shop where status=1 and censoring=1 and uid=` + c.Uid); g != nil {
		for _, v := range g {
			gid = append(gid, fmt.Sprint(v["id"]))
		}
	}
	if len(gid) == 0 {
		return
	}
	gids := strings.Join(gid, ",")
	var w []string
	status := c.GetString("status")
	statusajax := c.GetString("statusajax")
	if statusajax == "" {
		if status == "" {
			w = append(w, `a.status>0`)
		} else {
			w = append(w, `a.status=`+status)
		}
	} else {
		if statusajax == "0" {
			w = append(w, `a.status>0`)
		} else {
			w = append(w, `a.status=`+statusajax)
		}
	}
	w = append(w, `a.gid in(`+gids+`)`)
	ws := strings.Join(w, ` and `)
	page, _, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.status asc,a.paytime desc,a.addtime desc`
	sql := `select a.id,a.ordernum,a.uid,a.carid,a.price,a.money,a.addtime,a.address,a.invoice,a.count,a.content,a.status,a.name,a.mobile,a.address
		,b.name as yname,d.monicker,e.pathname,u.name as shen,i.name as shi,o.name as qu from mt_order a left join mt_member_coupon c on a.yid=c.id 
		left join mt_coupon b on c.yid=b.id left join mt_member d on a.uid=d.id left join mt_resources e on d.avatar=e.id left join mt_area u on 
		a.province=u.code left join mt_area i on a.city=i.code left join mt_area o on a.area=o.code where ` + ws + ob + lt
	info := models.Query(sql)
	for _, i := range info {
		carid := fmt.Sprint(i["carid"])
		shopcar := models.Query(`select a.num,a.status,a.id,a.sendnum,a.pid,a.spec as spec2,b.title,b.length,b.width,b.height,b.weight,b.number,b.spec,
				b.isw,d.monicker,d.mobile,c.pathname as mpath,e.pathname as path,s.name,s.address,s.tel,p.pathname as spath from mt_order_car a join 
				mt_product b on a.pid=b.id left join mt_shop s on b.gid=s.id left join mt_member d on a.did=d.id left join mt_resources
				 c on d.avatar=c.id left join mt_resources e on b.img=e.id left join mt_resources p on s.logo=p.id where a.id in(` + carid + `) 
				 order by a.paytime`)
		var we = "0.00"
		var volume = "0.00"
		for _, v := range shopcar {
			isw := fmt.Sprint(v["isw"])
			width := fmt.Sprint(v["width"])
			height := fmt.Sprint(v["height"])
			length := fmt.Sprint(v["length"])
			weight := fmt.Sprint(v["weight"])
			if isw == "0" {
				volume = sword.Sum(volume, sword.Mult(length, sword.Mult(height, width)))
			} else {
				we = sword.Sum(we, weight)
			}
		}
		i["product"] = shopcar
		i["we"] = we
		i["volume"] = sword.Divi(volume, "1000000")
		if i["status"].(string) == "6" {
			i["return"] = models.Query("select `id`,`agree`,`check` from mt_order_return where status=1 and oid=" + i["id"].(string))[0]
		}
	}
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["info"] = info
		js["uid"] = c.Uid
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["status"] = status
		c.Data["uid"] = c.Uid
		c.Data["info"] = info
		c.TplName = "wechat/my/order_manage.html"
	}
}
func (c *MyOrderManage) Post() {
	input := c.Input()
	input.Set("uid", c.Uid)
	if input.Get("id") == "" {
		input.Set("addtime", sword.Gettimes(""))
	}
	if _, err := models.Save("mt_order_invoice", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type MyInvoice struct {
	BaseController
}

func (c *MyInvoice) Get() {
	info := models.Query(`select * from mt_order_invoice where uid=` + c.Uid)
	if c.IsAjax() {
		if info != nil {
			c.Data["json"] = info[0]
		}
		c.ServeJSON()
	} else {
		if info != nil {
			c.Data["info"] = info[0]
		}
		c.TplName = "wechat/my/invoice.html"
	}
}
func (c *MyInvoice) Post() {
	input := c.Input()
	input.Set("uid", c.Uid)
	if input.Get("id") == "" {
		input.Set("addtime", sword.Gettimes(""))
	}
	if _, err := models.Save("mt_order_invoice", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	c.ServeJSON()
}

type MyOrderSend struct {
	BaseController
}

func (c *MyOrderSend) Get() {
	c.TplName = "wechat/my/order_send.html"
}
func (c *MyOrderSend) Post() {
	id := c.GetString("id")
	dname := c.GetString("name")
	dmobile := c.GetString("mobile")
	img := c.GetString("img")
	if is := models.Query(`select id,uid,carid,gid from mt_order where status=2 and id=` + id); is != nil {
		if err := models.Exec(fmt.Sprintf(`update mt_order set status=3,shippingtime='%v',drivername='%v',drivermobile='%v',sendimg='%v' where id=%v`,
			sword.Gettimes(""), dname, dmobile, img, id)); err != nil {
			c.Data["json"] = 0
			beego.Debug(err)
		} else {
			c.Data["json"] = 1
			carid := fmt.Sprint(is[0]["carid"])
			if fmt.Sprint(is[0]["gid"]) == "0" {
				if err := models.Exec(`update mt_order_car set status=4 where id in(` + carid + `)`); err != nil {
					beego.Debug(err)
				}
			} else {
				if err := models.Exec(`update mt_order_car set status=4,sendnum=num where id in(` + carid + `)`); err != nil {
					beego.Debug(err)
				}
			}
		}
	} else {
		c.Data["json"] = 0
	}
	c.ServeJSON()
}

type MyIntegral struct {
	BaseController
}

func (c *MyIntegral) Get() {
	integral := models.Query(`select integral from mt_member where id=` + c.Uid + ` limit 1`)[0]["integral"]
	page, _, num := models.Pagechange(c.GetString("page"), "")
	sb := sword.NewSqlBuilder()
	sb.Select("a.title", "a.id", "a.design", "a.integral", "b.pathname", "a.num")
	sb.From("mt_product_integral a")
	sb.LeftJoinOn("mt_resources b on a.img=b.id")
	sb.Where("a.status=1", "a.censoring=1")
	sb.OrderBy("a.sort desc")
	sb.Limit(num)
	sb.Offset(page)
	product := models.Query(sb.String())
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["integral"] = integral
		js["product"] = product
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["integral"] = integral
		c.Data["product"] = product
		c.TplName = "wechat/my/integral.html"
	}
}

type MyIntegralContent struct {
	BaseController
}

func (c *MyIntegralContent) Get() {
	product := models.Query(`select a.*,b.pathname from mt_product_integral a left join mt_resources b 
		on a.img=b.id where a.id=` + c.GetString("id") + ` limit 1`)[0]
	if c.IsAjax() {
		product["content"] = sword.UeditorChange(models.Urlc, fmt.Sprint(product["content"]))
		c.Data["json"] = product
		c.ServeJSON()
	} else {
		c.Data["product"] = product
		c.TplName = "wechat/my/integral_content.html"
	}
}
func (c *MyIntegralContent) Post() {
	defer c.ServeJSON()
	var id = c.GetString("id")
	pro := models.Query(`select num,integral from mt_product_integral where id=` + id)[0]
	number, _ := strconv.Atoi(pro["num"].(string))
	integral := pro["integral"].(string)
	dnumber, _ := strconv.Atoi(models.Query(`select count(id) as number from mt_member_integral where pid=` + id)[0]["number"].(string))
	if number > dnumber {
		if en := models.Query(`select id from mt_member where integral>` + integral + ` and id=` + c.Uid); en != nil {
			o := models.Orm()
			o.Begin()
			if _, err := o.Raw(`update mt_member set integral=integral-` + integral + ` where id=` + c.Uid).Exec(); err != nil {
				beego.Debug(err)
				o.Rollback()
				c.Data["json"] = 0
				return
			}
			if _, err := o.Raw(`insert mt_member_integral (uid,type,number,addtime,pid) values (` + c.Uid + `,1,` + integral + `,'` + sword.Gettimes("") + `',` + id + `)`).Exec(); err != nil {
				beego.Debug(err)
				o.Rollback()
				c.Data["json"] = 0
				return
			}
			o.Commit()
			c.Data["json"] = 1
		} else {
			c.Data["json"] = 3
		}
	} else {
		c.Data["json"] = 2
	}
}

type MyIntegralRule struct {
	BaseController
}

func (c *MyIntegralRule) Get() {
	help := models.Query(`select id,name,content from mt_page where cid=294 and status=1 order by sort desc,addtime desc limit 1`)
	if help == nil {
		return
	}
	if c.IsAjax() {
		c.Data["json"] = help[0]
		c.ServeJSON()
	} else {
		c.Data["info"] = help[0]
		c.TplName = "wechat/my/integral_rule.html"
	}
}

type MyIntegralList struct {
	BaseController
}

func (c *MyIntegralList) Get() {
	integral := models.Query(`select integral from mt_member where id=` + c.Uid + ` limit 1`)[0]["integral"]
	page, _, num := models.Pagechange(c.GetString("page"), "20")
	sb := sword.NewSqlBuilder()
	sb.Select("a.type", "a.id", "a.addtime", "a.number", "b.title", "c.ordernum")
	sb.From("mt_member_integral a")
	sb.LeftJoinOn("mt_product_integral b on a.pid=b.id", "mt_order c on a.oid=c.id")
	sb.Where("a.uid=" + c.Uid)
	sb.OrderBy("a.addtime desc")
	sb.Limit(num)
	sb.Offset(page)
	info := models.Query(sb.String())
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["integral"] = integral
		js["info"] = info
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["integral"] = integral
		c.Data["info"] = info
		c.TplName = "wechat/my/integral_list.html"
	}
}

type MyMyshop struct {
	BaseController
}

func (c *MyMyshop) Get() {
	shop := models.Query(`select a.id,a.name,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where a.status=1 and a.censoring=1 and style=1 and a.uid=` + c.Uid)
	var id string
	if shop != nil {
		id = fmt.Sprint(shop[0]["id"])
	}
	if c.Uid == models.Systems()["uid"].(string) {
		id = "0"
	}
	if id != "" {
		var pid []string
		var psql string
		if id == "0" {
			psql = `select a.id from mt_product a left join mt_shop b on a.gid=b.id where a.status=1 and a.censoring=1 and (a.gid=0 or b.style=0)`
		} else {
			psql = `select id from mt_product where status=1 and censoring=1 and gid=` + id
		}
		product := models.Query(psql)
		for _, v := range product {
			pid = append(pid, v["id"].(string))
		}
		pids := strings.Join(pid, ",")
		if pids == "" {
			pids = "''"
		}
		now := time.Now().Local()
		week := int(now.Weekday())
		if week == 0 {
			week = 7
		}
		var weektime []string
		for i := 1; i <= week; i++ {
			count := week - i
			weeks := now.AddDate(0, 0, -count)
			weektime = append(weektime, weeks.Format("2006-01-02")+" 00:00:00")
		}
		var rnum []string
		loc, _ := time.LoadLocation("Local")
		for _, v := range weektime {
			ttime, _ := time.ParseInLocation(Tparse, v, loc)
			dtime := ttime.AddDate(0, 0, 1)
			v2 := dtime.Format("2006-01-02") + ` 00:00:00`
			rnumi := models.Query(`select count(a.id) as num from mt_systems_reading a where (a.gid=` + id + ` or pid in(` + pids + `))` + `
					 and a.addtime>='` + v + `' and a.addtime<'` + v2 + `'`)[0]["num"]
			rnum = append(rnum, rnumi.(string))
		}
		ttimes := now.Format("2006-01-02")
		t2 := now.AddDate(0, 0, 1)
		dtimes := t2.Format("2006-01-02")
		ttimes += ` 00:00:00`
		dtimes += ` 00:00:00`
		order := models.Query(`select count(a.id) as num,sum(a.money) as moneysum from mt_order a where a.status>1 and a.status<6 and 
					a.addtime>='` + ttimes + `' and a.addtime<'` + dtimes + `' and a.gid=` + id)
		onum := order[0]["num"]
		omoney := ``
		if order[0]["moneysum"] != nil {
			omoney = order[0]["moneysum"].(string)
		} else {
			omoney = "0"
		}
		onum2 := models.Query(`select count(a.id) as num from mt_order a where a.status=2 and a.gid=` + id)[0]["num"]
		onum3 := models.Query(`select count(a.id) as num from mt_order a where a.status=3 and a.gid=` + id)[0]["num"]
		onum6 := models.Query(`select count(a.id) as num from mt_order a where a.status=6 and a.gid=` + id)[0]["num"]
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = shop
			js["rnum"] = rnum
			js["onum"] = onum
			js["onum2"] = onum2
			js["onum3"] = onum3
			js["onum6"] = onum6
			js["omoney"] = omoney
			js["rnumn"] = rnum[len(rnum)-1]
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = shop
			c.Data["rnum"] = rnum
			c.Data["onum"] = onum
			c.Data["onum2"] = onum2
			c.Data["onum3"] = onum3
			c.Data["onum6"] = onum6
			c.Data["omoney"] = omoney
			c.Data["rnumn"] = rnum[len(rnum)-1]
			c.TplName = "wechat/my/myshop.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my")
		}
	}
}

type MyShopAdd struct {
	BaseController
}

func (c *MyShopAdd) Get() {
	if is := models.Query(`select a.id,a.type,a.logo,a.intr,a.name,a.businesslicense,a.person,a.license,a.cardimg,a.theimg,a.province,a.city,a.area,a.tel,
		b.pathname as lpath,c.pathname as cpath,d.pathname as tpath,e.pathname,f.name as shen,g.name as shi,h.name as qu from mt_shop_add a 
		left join mt_resources b on a.license=b.id left join mt_resources c on a.cardimg=c.id left join mt_resources d on a.theimg=d.id left join
		 mt_resources e on a.logo=e.id left join mt_area f on a.province=f.code left join mt_area g on a.city=g.code left join mt_area h on a.area=h.code
		  where a.uid=` + c.Uid + ` and censoring=2`); is != nil {
		c.Data["json"] = is[0]
	}
	if c.IsAjax() {
		c.ServeJSON()
	} else {
		c.TplName = "wechat/my/shop_add.html"
	}
}
func (c *MyShopAdd) Post() {
	defer c.ServeJSON()
	input := c.Input()
	id := input.Get("id")
	if id == "" {
		input.Set("addtime", sword.Gettimes(""))
		if is := models.Query(`select id from mt_shop_add where uid=` + c.Uid); is != nil {
			c.Data["json"] = 2
			return
		}
	}
	input.Set("uid", c.Uid)
	input.Set("censoring", "0")
	if _, err := models.Save("mt_shop_add", input); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
		m := models.Query(`select monicker from mt_member where id=` + c.Uid)[0]
		openid := models.Query(`select b.wx_openid_gzh from mt_systems a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"].(string)
		data2 := map[string]map[string]interface{}{
			"first":    {"value": `您有一个新的商户申请`},
			"keyword1": {"value": m["monicker"], "color": "#e4393c"},
			"keyword2": {"value": c.GetString("name")},
			"keyword3": {"value": sword.Gettimes(""), "color": "#9c9c9c"},
			"remark":   {"value": `请您尽快处理~`},
		}
		models.Push(openid, `JFrrx9CxZwepPyYz6VdwLRFa1e5xFbof3-DEGhtoEsI`, data2, "", "")
	}
}

type MyShopData struct {
	BaseController
}

func (c *MyShopData) Get() {
	shop := models.Query(`select a.id,a.name,b.pathname from mt_shop a left join mt_resources b on a.logo=b.id where a.status=1 and a.censoring=1 and a.uid=` + c.Uid)
	var id string
	if shop != nil {
		id = fmt.Sprint(shop[0]["id"])
	}
	if c.Uid == models.Systems()["uid"].(string) {
		id = "0"
	}
	if id != "" {
		var pid []string
		var psql string
		if id == "0" {
			psql = `select a.id from mt_product a left join mt_shop b on a.gid=b.id where a.status=1 and a.censoring=1 and (a.gid=0 or b.style=0)`
		} else {
			psql = `select id from mt_product where status=1 and censoring=1 and gid=` + id
		}
		product := models.Query(psql)
		for _, v := range product {
			pid = append(pid, v["id"].(string))
		}
		pids := strings.Join(pid, ",")
		if pids == "" {
			pids = "''"
		}
		now := time.Now().Local()
		week := int(now.Weekday())
		var weektime []string
		for i := 1; i <= week; i++ {
			count := week - i
			weeks := now.AddDate(0, 0, -count)
			weektime = append(weektime, weeks.Format("2006-01-02")+" 00:00:00")
		}
		var rnum []string
		var onum []string
		var mnum []string
		var ts []string
		loc, _ := time.LoadLocation("Local")
		for _, v := range weektime {
			ttime, _ := time.ParseInLocation(Tparse, v, loc)
			ts = append(ts, ttime.Format(`01-02`))
			dtime := ttime.AddDate(0, 0, 1)
			v2 := dtime.Format("2006-01-02") + ` 00:00:00`
			rnumi := models.Query(`select count(a.id) as num from mt_systems_reading a where (a.gid=` + id + ` or pid in(` + pids + `))` + `
					 and a.addtime>='` + v + `' and a.addtime<'` + v2 + `'`)[0]["num"]
			rnum = append(rnum, rnumi.(string))
			order := models.Query(`select count(a.id) as num,sum(a.money) as money from mt_order a where a.gid=` + id + ` and a.paytime>=
				'` + v + `' and a.paytime<'` + v2 + `'`)[0]
			onumi := order["num"]
			onum = append(onum, onumi.(string))
			mnumi, ok := order["money"].(string)
			if !ok {
				mnumi = "0.00"
			}
			mnum = append(mnum, mnumi)
		}

		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["ts"] = ts
			js["rnum"] = rnum
			js["onum"] = onum
			js["mnum"] = mnum
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["ts"] = ts
			c.Data["rnum"] = rnum
			c.Data["onum"] = onum
			c.Data["mnum"] = mnum
			c.TplName = "wechat/my/shop_data.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my")
		}
	}
}

type MyShopProduct struct {
	BaseController
}

func (c *MyShopProduct) Get() {
	shop := models.Query(`select id from mt_shop where censoring=1 and status=1 and uid=` + c.Uid)
	var sid string
	if shop != nil {
		sid = fmt.Sprint(shop[0]["id"])
	}
	if c.Uid == models.Systems()["uid"].(string) {
		sid = "0"
	}
	if sid == "" {
		return
	} else {
		var w []string
		w = append(w, `a.status=1`)
		if sid == "0" {

			w = append(w, `(a.gid=`+sid+` or e.style=0)`)
		} else {
			w = append(w, `a.gid=`+sid)
		}
		cid := c.GetString("cid")
		if cid != "" {
			w = append(w, `a.gcid=`+cid)
		}
		ws := strings.Join(w, ` and `)
		page, _, num := models.Pagechange(c.GetString("page"), "")
		lt := ` limit ` + page + `,` + num
		ob := ` order by a.censoring desc,a.recommend desc`
		sql := `select a.title,a.censoring,a.keywords,a.price,a.gcid,a.id,a.sellnum,a.issellnum,b.pathname,c.name as gname,d.name as shen 
		from mt_product a left join mt_resources b on a.img=b.id left join mt_shop c on a.gid=c.id left join mt_shop e on a.gid=e.id 
	left join mt_area d on c.province=d.id where ` + ws + ob + lt
		info := models.Query(sql)
		for _, v := range info {
			info_keyword := fmt.Sprint(v["keywords"])
			info_keyword = strings.Trim(info_keyword, ",")
			if info_keyword == "" {
				info_keyword = "''"
			}
			v["keywords"] = models.Query(`select title from mt_keywords where id in (` + info_keyword + `)  and status=1 and type=1`)
			sellnum := models.Query(`select sum(a.num) as num from mt_order_car a where a.status=2 and a.pid=` + v["id"].(string))[0]["num"]
			if sellnums, ok := sellnum.(string); ok {
				v["sellnum"] = sellnums
			} else {
				v["sellnum"] = "0"
			}
		}
		if c.IsAjax() {
			var js = make(map[string]interface{})
			js["info"] = info
			if c.GetString("page") == "" {
				js["column"] = models.Query(`select id,name from mt_column where type='p' and pid=0 and gid=` + sid)
			}
			c.Data["json"] = js
			c.ServeJSON()
		} else {
			c.Data["info"] = info
			c.Data["column"] = models.Query(`select id,name from mt_column where type='p' and pid=0 and gid=` + sid)
			c.TplName = "wechat/my/shop_product.html"
		}
	}
}

type MyShopProductDel struct {
	BaseController
}

func (c *MyShopProductDel) Get() {
	id := c.GetString("id")
	if isp := models.Query(`select a.id from mt_product a where a.status=1 and a.id=` + id); isp != nil {
		status := c.GetString("status")
		sql := ``
		var js = make(map[string]interface{})
		if status == "1" {
			js["censoring"] = 0
			sql = `update mt_product set censoring=0 where id=` + id
		} else {
			js["censoring"] = 1
			sql = `update mt_product set censoring=1 where id=` + id
		}
		if err := models.Exec(sql); err != nil {
			js["status"] = 0
			beego.Debug(err)
		} else {
			js["status"] = 1
		}
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.StopRun()
	}
}

func (c *MyShopProductDel) Post() {
	id := c.GetString("id")
	if isp := models.Query(`select a.id from mt_product a where a.status=1 and a.id=` + id); isp != nil {
		if err := models.Exec(`update mt_product set status=0 where id=` + id); err != nil {
			c.Data["json"] = 0
			beego.Debug(err)
		} else {
			c.Data["json"] = 1
		}
		c.ServeJSON()
	} else {
		c.StopRun()
	}
}

type MyShopOrderCancle struct {
	BaseController
}

func (c *MyShopOrderCancle) Get() {
	id := c.GetString("id")
	beego.Debug(id)
	if isp := models.Query(`select a.id from mt_order a where a.status=1 and a.id=` + id); isp != nil {
		if err := models.Exec(`update mt_order set status=0 where id=` + id); err != nil {
			beego.Debug(err)
			c.Data["json"] = 0
		} else {
			c.Data["json"] = 1
		}
		c.ServeJSON()
	} else {
		c.StopRun()
	}
}

type MyShopOrderReturn struct {
	BaseController
}

func (c *MyShopOrderReturn) Get() {
	id := c.GetString("id")
	if re := models.Query(`select a.*,b.name as whys,c.ordernum,c.carid,c.uid,c.money,c.mobile from mt_order_return a left join mt_systems_configuration b on a.why=b.id
		 left join mt_order c on a.oid=c.id where a.status=1 and a.id=` + id); re != nil {
		for _, v := range re {
			v["gid"] = models.Query(`select b.gid from mt_order_car a join mt_product b on a.pid=b.id where a.id in(` + fmt.Sprint(v["carid"]) + `)`)[0]["gid"]
			v["product"] = models.Query(`select b.model,a.num,b.id,b.price,b.title,b.number,b.spec,c.pathname,a.spec as spec2 from mt_order_car a left join mt_product b on a.pid=b.id left join mt_resources c 
		 			on b.img=c.id where a.id in(` + fmt.Sprint(v["carid"]) + `)`)
		}
		if c.IsAjax() {
			// var js = make(map[string]interface{})
			// js["info"] = order[0]
			// js["why"] = why
			c.Data["json"] = re[0]
			c.ServeJSON()
		} else {
			c.Data["info"] = re[0]
			c.TplName = "wechat/my/shop_order_return.html"
		}
	} else {
		if c.IsAjax() {
			c.StopRun()
		} else {
			c.Ctx.Redirect(302, "/mobile/my")
		}
	}
}
func (c *MyShopOrderReturn) Post() {
	defer c.ServeJSON()
	t := c.GetString("type")
	id := c.GetString("id")
	status := c.GetString("status")
	if err := models.Exec("update mt_order_return set `" + t + "`=" + status + " where id=" + id); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
	} else {
		c.Data["json"] = 1
	}
	var first string
	if t == "check" {
		if status == "1" {
			first = `商家通过了对您的退款货物检查,请等待打款`
		} else {
			first = `您的货物检查未通过,请联系商家`
		}
	} else {
		if status == "1" {
			first = `您的退款申请已被商家接受`
		} else {
			first = `您的退款申请已被商家拒绝,您可以修改申请或与商家联系`
		}
	}
	m := models.Query(`select b.addtime,b.money,b.ordernum,c.id,c.monicker,c.wx_openid_gzh from mt_order_return a left join mt_order b on a.oid=b.id left join mt_member c on b.uid=c.id where a.id=` + id)[0]
	mopenid := fmt.Sprint(m["wx_openid_gzh"])
	data1 := map[string]map[string]interface{}{
		"first":    {"value": first},
		"keyword1": {"value": m["ordernum"], "color": `#36353A`},
		"keyword2": {"value": m["money"], "color": `#e31939`},
		"keyword3": {"value": m["addtime"], "color": "#36353A"},
		"remark":   {"value": `您可以进入商城查看~`},
	}
	models.Push(mopenid, `L-BI2hpqu5MJ5sI0Rm8-j1jeTMmGAnU_oEYphIUPvCE`, data1, models.Urlc+"/mobile/my/order_return_detail?id="+id, "")
}

type MyShopOrderReturnPay struct {
	BaseController
}

func (c *MyShopOrderReturnPay) Post() {
	defer c.ServeJSON()
	id := c.GetString("id")
	money := c.GetString("money")
	moneyf, err := strconv.ParseFloat(money, 64)
	money = fmt.Sprintf(`%.2f`, moneyf)
	if err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
		return
	}
	if isen := models.Query(`select id from mt_member where money>=` + money + ` and id=` + c.Uid); isen == nil {
		c.Data["json"] = 2
		return
	}
	nowtime := sword.Gettimes("")
	o := models.Orm()
	o.Begin()
	g := models.Query(`select a.name,b.monicker,b.wx_openid_gzh from mt_shop a left join mt_member b on a.uid=` + c.Uid + ` and a.status=1 and a.censoring=1`)[0]
	m := models.Query(`select b.addtime,b.money,b.ordernum,c.id,c.monicker,c.wx_openid_gzh from mt_order_return a left join mt_order b on a.oid=b.id left join mt_member c on b.uid=c.id where a.id=` + id)[0]
	mopenid := fmt.Sprint(m["wx_openid_gzh"])
	uid := fmt.Sprint(m["id"])
	if _, err := o.Raw(`update mt_member set money=money-` + money + ` where id=` + c.Uid).Exec(); err != nil {
		beego.Debug(err)
		o.Rollback()
		c.Data["json"] = 0
		return
	}
	if _, err := o.Raw(`insert into mt_member_money_detail (uid,type,money,addtime) values ('%v','%v','%v','%v')`, c.Uid, 7, money, nowtime).Exec(); err != nil {
		beego.Debug(err)
		o.Rollback()
		c.Data["json"] = 0
		return
	}
	if _, err := o.Raw(`update mt_member set money=money+` + money + ` where id=` + uid).Exec(); err != nil {
		beego.Debug(err)
		o.Rollback()
		c.Data["json"] = 0
		return
	}
	if _, err := o.Raw(`insert into mt_member_money_detail (uid,type,money,addtime) values ('%v','%v','%v','%v')`, uid, 6, money, nowtime).Exec(); err != nil {
		beego.Debug(err)
		o.Rollback()
		c.Data["json"] = 0
		return
	}
	if _, err := o.Raw(`update mt_order_return set returnmoney=` + money + `,return=1,returntime=` + nowtime + ` where id=` + id).Exec(); err != nil {
		beego.Debug(err)
		o.Rollback()
		c.Data["json"] = 0
		return
	}
	data1 := map[string]map[string]interface{}{
		"first":    {"value": fmt.Sprintf(`您收到%v的一笔退款,金额为%v`, g["name"], money)},
		"keyword1": {"value": m["ordernum"], "color": `#36353A`},
		"keyword2": {"value": m["money"], "color": `#e31939`},
		"keyword3": {"value": m["addtime"], "color": "#36353A"},
		"remark":   {"value": `您可以进入商城查看~`},
	}
	models.Push(mopenid, `L-BI2hpqu5MJ5sI0Rm8-j1jeTMmGAnU_oEYphIUPvCE`, data1, models.Urlc+"/mobile/my/order_return_detail?id="+id, "")
}

type MyOrderPush struct {
	BaseController
}

func (c *MyOrderPush) Get() {
	defer c.ServeJSON()
	id := c.GetString("id")
	now := sword.Gettimes("2006-01-02") + " 00:00:00"
	c.Data["json"] = 1
	if is := models.Query(`select id from mt_order_push where oid=` + id + ` and addtime>'` + now + `'`); is != nil {
		c.Data["json"] = 2
		return
	}
	if err := models.Exec(`insert into mt_order_push (oid,addtime) values(` + id + `,'` + sword.Gettimes("") + `')`); err != nil {
		beego.Debug(err)
		c.Data["json"] = 0
		return
	}
	order := models.Query(`select a.ordernum,a.gid,a.money from mt_order a where a.id=` + id)[0]
	data1 := map[string]map[string]interface{}{
		"first":    {"value": `您收到了一条催单提醒`},
		"keyword1": {"value": order["ordernum"], "color": `#36353A`},
		"keyword2": {"value": `待发货`, "color": `#e31939`},
		"keyword3": {"value": order["money"], "color": "#36353A"},
		"remark":   {"value": `请您尽快发货~`},
	}
	var gid = fmt.Sprint(order["gid"])
	var openid string
	if gid == "0" {
		openid = fmt.Sprint(models.Query(`select b.wx_openid_gzh from mt_systems a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"])
	} else {
		openid = fmt.Sprint(models.Query(`select b.wx_openid_gzh from mt_shop a left join mt_member b on a.uid=b.id`)[0]["wx_openid_gzh"])
	}
	models.Push(openid, `vxLbkepVf6pnXs3__9sIspnGAW-PK2woD7BQFki8pyA`, data1, models.Urlc+"/mobile/my/order_manage", "")
}
